/*
 * Copyright 2007 David R Kirkland 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

using System;
using System.IO;
using NetSpider.Retrieval.Policies;
using NetSpider;

namespace NetSpider.Core
{
    internal class SpiderImpl : ISpider
    {
        private bool _disposed = false;

        public SpiderImpl()
        {
            //_fetcherPolicyFactory = DependencyResolver.GetImplementationOf<IFetcherPolicyFactory>();
        }

        public IContent RetrieveContent(string uri)
        {
            SingleItemRetrievalStrategy singleItemStrategy = new SingleItemRetrievalStrategy();
            IMonkey monkey = DependencyResolver.GetImplementationOf<IMonkey>();
            monkey.Start(uri, singleItemStrategy);
            return singleItemStrategy.FetchedContent;
        }

        public IContent RetrieveContent(Uri uri)
        {
            throw new NotImplementedException();
        }

        public IContentTree RetrieveContent(string startUri, int maxBatchSize)
        {
            BatchRetrievalStrategy batchStrategy = new BatchRetrievalStrategy(maxBatchSize);
            IMonkey monkey = DependencyResolver.GetImplementationOf<IMonkey>();
            monkey.Start(startUri, batchStrategy);
            return batchStrategy.FetchedContent;
        }

        public void StartCustomSearch(string startUri, ISearchStrategy strategy)
        {
            IMonkey monkey = DependencyResolver.GetImplementationOf<IMonkey>();
            monkey.Start(startUri, strategy);
        }

        public IContentTree RetrieveContent(Uri startUri, int batchSize)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                FileHelper.RemoveFolder(Config.TemporaryFolder);
                _disposed = true;
            }
        }

        ~SpiderImpl()
        {
            Dispose(false);
        }
    }
}